<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class Tech extends Controller
{
    use \App\Traits\CacheTrait, \App\Traits\TechTrait, \App\Traits\SessionTrait;

    public function techList(Request $request)
    {

        if ($request->method() == 'POST') {
            $techName = $request->input('name', '');
            $techDomain2 = $request->input('domain2', -1);
            $status = $request->input('status', 0);
            $pageNumber = $request->input('page', 1);
            $domainList = [];
            $checkStatusList = [];
        } else {
            $techName = '';
            $techDomain2 = -1;
            $status = -1;
            $pageNumber = 1;
            $otherDicList = $this->cacheGetOtherDicList();
            $domainList = $otherDicList[config('constant.domain_dic')][0];
            $checkStatusList = config('constant.device_check_status');
        }
        $pageLimit = 10;

        $postData = [
            'company_id' => $this->sessionReturnCompanyId(),
            'tech_name' => $techName,
            'domain2' => $techDomain2,
            'status' => $status,
            'page_number' => $pageNumber,
            'page_limit' => $pageLimit,
        ];

        $result = $this->getTechList(parent::CODE_GET_TECH_LIST, $postData);
        $result = $result ? $result : [];
        $viewData = [
            'pageCount' => $result['pageCount'],
            'dataList' => $result['dataList'],
            'currentPage' => $pageNumber,
            'select_info' => 'tech',
            'domain_list' => $domainList,
            'check_status_list' => $checkStatusList,
        ];

        return $request->method() == 'POST' ? view('admin/tech/tech_list_post', $viewData) : view('admin/tech/tech_list', $viewData);
    }

    public function addTech()
    {
        $otherDicList = $this->cacheGetOtherDicList();
        $domainList = $otherDicList[config('constant.domain_dic')][0];
        $domainList2 = $otherDicList[config('constant.domain_dic')][$domainList[0]['id']];
        $patentTypeList = config('constant.patent_type');
        $viewData = [
            'select_info' => 'tech',
            'domain_list1' => $domainList,
            'domain_list2' => $domainList2,
            'patent_type_list' => $patentTypeList,
        ];
        return view('admin/tech/add_tech', $viewData);
    }

    public function editTech(Request $request)
    {
        $techId = $request->input('id');
        $otherDicList = $this->cacheGetOtherDicList();
        $domainList = $otherDicList[config('constant.domain_dic')][0];
        $domainList2 = $otherDicList[config('constant.domain_dic')][$domainList[0]['id']];
        $patentTypeList = config('constant.patent_type');
        $otherDicList2 = $this->cacheGetOtherDicNameList();

        $postData = [
            'company_id' => $this->sessionReturnCompanyId(),
            'id' => $techId,
        ];
        $result = $this->getTechInfo(parent::CODE_GET_TECH_INFO, $postData);
        $result = $result ? $result : [];
        if ($result) {
            $viewData = [
                'select_info' => 'tech',
                'dataInfo' => $result,
                'domain_list1' => $domainList,
                'domain_list2' => $domainList2,
                'patent_type_list' => $patentTypeList,
                'getNameList' => $otherDicList2,
                'tech_id' => $techId,
            ];
            return view('admin/tech/edit_tech', $viewData);
        }
    }

    public function viewTech(Request $request)
    {
        $techId = $request->input('id');
        $otherDicList2 = $this->cacheGetOtherDicNameList();
        $postData = [
            'company_id' => $this->sessionReturnCompanyId(),
            'id' => $techId,
        ];
        $result = $this->getTechInfo(parent::CODE_GET_TECH_INFO, $postData);
        $result = $result ? $result : [];
        if ($result) {
            $viewData = [
                'select_info' => 'tech',
                'dataInfo' => $result,
                'getNameList' => $otherDicList2,
            ];
            return view('admin/tech/view_tech', $viewData);
        }
    }

}
